package com.service;

import com.dao.UserdetailedDao;
import com.dao.UserinfoDao;
import com.pojo.Userdetailed;
import com.pojo.Userinfo;
import com.util.MyBatisHelp;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserManagerService {

    /**
     * 用户登录业务
     * @param account
     * @param password
     * @return
     */
    public Userinfo userLogin(String account,String password){
        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);

        Userinfo userinfo = userinfoDao.queryByAccount(account);

        //password = 加密方法(password);

        //检查用户是否存在，且密码正确
        if(userinfo!=null&&userinfo.getPassword().equals(password)){
            return userinfo;
        }
        return null;
    }

    /**
     * 用账号查询用户
     * @param account
     * @return
     */
    public Userinfo queryUserByAccount(String account) {
        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);
        return userinfoDao.queryByAccount(account);
    }

    /**
     * 通过用户id查询用户
     * @param id
     * @return
     */
    public Userinfo queryUserInfo(Long id) {
        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);
        Userinfo userinfo = userinfoDao.queryById(id);
        if(userinfo.getUd()==null){
            userinfo.setUd(new Userdetailed());
        }
        return userinfo;
    }

    /**
     * 用户注册
     * @param userinfo
     * @return
     */
    public int userReg(Userinfo userinfo) {
        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);
        try{
            Userinfo tempUserinfo = userinfoDao.queryByAccount(userinfo.getAccount());
            if(tempUserinfo==null){
                if(userinfoDao.insert(userinfo)==1){
                    session.commit();
                    return 1;
                }else{
                    session.rollback();
                    return -1;
                }
            }else{
                return 0;
            }
        }catch (TooManyResultsException e){ //账号存在多个，需要修改数据
            return 0;
        }


    }

    @Test
    public void testMain(){
        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);
        Userinfo userinfo = userinfoDao.queryById(1L);
        System.out.println(userinfo);
    }

    /**
     * 修改用户详细信息
     * @param userdetailed
     * @return
     */
    public boolean updateUserdetailed(Userdetailed userdetailed) {
        SqlSession session = MyBatisHelp.getSession();
        UserdetailedDao userdetailedDao = session.getMapper(UserdetailedDao.class);
        List<Userdetailed> list = new ArrayList<>();
        list.add(userdetailed);
        int num = userdetailedDao.insertOrUpdateBatch(list);
        if(num>0){
            session.commit();
            return true;
        }
        session.rollback();
        return false;
    }

    /**
     * ajax查询用户信息
     * @param userinfo
     * @return
     */
    public List<Userinfo> queryUserInfoList(Userinfo userinfo) {
        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);

        List<Userinfo> userinfoList = userinfoDao.queryAllBy(userinfo);
        return userinfoList;
    }

    /**
     * axios查询用户
     * @return
     */
    public List<Userinfo> queryAllUser(Userinfo userinfo) {

        SqlSession session = MyBatisHelp.getSession();
        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);
        List<Userinfo> userinfoList = userinfoDao.queryAllUserBy(userinfo);

        return userinfoList;
    }

    @Test
    public void testUserinfolist(){
        Userinfo userinfo = new Userinfo();
        userinfo.setAccount("zh");
        List<Userinfo> userinfoList = this.queryUserInfoList(userinfo);
        System.out.println(userinfoList);
    }

}
